﻿using System;
using System.Data.EntityClient;
using System.Data.SqlClient;

namespace PuanSunucusu
{
    public enum BaglantiTipleri
    {
        Mikro, Inter
    }

    public static class Function
    {
        public static EntityConnection Create(this EntityConnection source)
        {
            string serverName = "";
            string databaseName = "";
            string auth = "";
            string username = "";
            string password = "";

            serverName = Program.Session[Settings.InterServerName].ToString();
            databaseName = Program.Session[Settings.InterDatabaseName].ToString();
            auth = Program.Session[Settings.InterAuthenticateMode].ToString();
            username = Program.Session[Settings.InterUserName].ToString();
            password = Program.Session[Settings.InterPassword].ToString();

            string providerName = "System.Data.SqlClient";
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            sqlBuilder.MultipleActiveResultSets = true;
            if (auth == "Sql")
            {
                sqlBuilder.UserID = username;
                sqlBuilder.Password = password;
                sqlBuilder.IntegratedSecurity = false;
            }
            else
                sqlBuilder.IntegratedSecurity = true;

            string providerString = sqlBuilder.ToString();
            EntityConnectionStringBuilder entitycon = new EntityConnectionStringBuilder();
            entitycon.Provider = providerName;
            entitycon.ProviderConnectionString = providerString;
            entitycon.Metadata = @"res://*/PromoModel.csdl|res://*/PromoModel.ssdl|res://*/PromoModel.msl";

            source.ConnectionString = entitycon.ToString();
            return source;
        }
        public static string ToFormattedString(this DateTime source)
        {
            return string.Format("{0}-{1}-{2} {3}:{4}:{5}", source.Month.ToString("00"), source.Day.ToString("00"), source.Year.ToString("0000"), source.Hour.ToString("00"), source.Minute.ToString("00"), source.Second.ToString("00"));
        }
        public static string AddZeroLeft(this double source, int count)
        {
            string val = source.ToString("n2");
            return val.PadLeft(count, '0').Replace(',', '.');
        }
        public static string AddZeroRight(this double source, int count)
        {
            string val = source.ToString("n2");
            return val.PadRight(count, '0').Replace(',', '.');
        }

        public static string Mid(this string value, int start, int lenght)
        {
            if (value.Length > start + lenght)
            {
                return value.Substring(start, lenght);
            }
            else
            {
                return value.Substring(start);
            }
        }
    }

    public class ComboItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }
}
